package chapter14;

import chapter11.Role;
import chapter13.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-04-17 下午5:01
 */
@Controller("myController")
@RequestMapping("/home")
public class MyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/hello")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        return mv;
    }

    @RequestMapping("/hello2")
    public ModelAndView index2(@RequestParam("id") Long id) {
        LOGGER.info("param id:{}", id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("page2");
        return mv;
    }

    @RequestMapping("/user")
    public ModelAndView index2(@RequestParam("name") String name, @RequestParam(value = "note", required = false, defaultValue = "default") String note) {
        LOGGER.info("param name:{}, note:{}", name, note);
        Role r = new Role();
        r.setRoleName(name);
        r.setNote(note);
//        boolean result = userService.addUser(r);
//        LOGGER.info("Add user success?{}", result);
        ModelAndView mv = new ModelAndView();
        mv.addObject("role", r);
        mv.setViewName("roleDetails");
        return mv;
    }

    @RequestMapping(value = "/getRole", method = RequestMethod.GET)
    public ModelAndView getRole(@RequestParam("id") Long id) {
        LOGGER.info("param id:{}", id);
        Role r = userService.getRole(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("roleDetails");
        mv.addObject("role", r);
//        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
}
